package com.i2f.i2msk.img;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.IoUtil;
import com.i2f.i2msk.Config;

public class KeyboardImg {
	public static Map<String, Object> imageCache = new HashMap<String, Object>();

	static {
		try {
			ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
			InputStream inputStream = KeyboardImg.class.getResourceAsStream("/keyboard/numberKeyboard.png");
			IoUtil.copy(inputStream, byteArray);
			imageCache.put("numberKeyboard", byteArray.toByteArray());
			byteArray.close();
			inputStream.close();

			inputStream = KeyboardImg.class.getResourceAsStream("/keyboard/yLetterKeyboard.png");
			byteArray = new ByteArrayOutputStream();
			IoUtil.copy(inputStream, byteArray);
			imageCache.put("yLetterKeyboard", byteArray.toByteArray());
			byteArray.close();
			inputStream.close();

			inputStream = KeyboardImg.class.getResourceAsStream("/keyboard/XLetterKeyboard.png");
			byteArray = new ByteArrayOutputStream();
			IoUtil.copy(inputStream, byteArray);
			imageCache.put("XLetterKeyboard", byteArray.toByteArray());
			byteArray.close();
			inputStream.close();

			inputStream = KeyboardImg.class.getResourceAsStream("/keyboard/numberKeyboard1.png");
			byteArray = new ByteArrayOutputStream();
			IoUtil.copy(inputStream, byteArray);
			imageCache.put("numberKeyboard1", byteArray.toByteArray());
			byteArray.close();
			inputStream.close();

			inputStream = KeyboardImg.class.getResourceAsStream("/keyboard/symbolKeyboard.png");
			byteArray = new ByteArrayOutputStream();
			IoUtil.copy(inputStream, byteArray);
			imageCache.put("symbolKeyboard", byteArray.toByteArray());
			byteArray.close();
			inputStream.close();
			
			for (int i = 0; i < 10; i++) {
				inputStream = KeyboardImg.class.getResourceAsStream("/keyboard/number/"+i+".png");
				BufferedImage image = ImageIO.read(inputStream);
				imageCache.put("number_"+i, image);
				inputStream.close();
			}
		} catch (IOException e) {
			throw new RuntimeException("键盘图片文件错误。无法读取", e);
		}
	}

	public static String createNumberKeyboard(List<Integer> num) throws IOException {
		BufferedImage image = ImageIO.read(new ByteArrayInputStream((byte[]) imageCache.get("numberKeyboard")));
		int index = 0;
		int w = 122;
		int h = 58;
		Graphics g = image.getGraphics();
		g.setColor(Color.black);
		g.setFont(new Font("Arial", 1, Config.numberFontSize));
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				g.drawImage((BufferedImage)imageCache.get("number_"+num.get(index)), j * w + (j * 2)+2, i * h+3 +(i*3), null);
				index++;
			}
		}
		g.drawImage((BufferedImage)imageCache.get("number_"+num.get(index)), w+4, 3 * h +12, null);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ImageIO.write(image, "png", out);
		byte[] b = out.toByteArray();
		return Base64.encode(b);
	}

	protected static final List<Integer> numberKey = Arrays.asList(new Integer[] {0,1,2,3,4,5,6,7,8,9 });
	protected static final List<Character> yLetterKey = Arrays.asList(new Character[] {'q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m' });
	protected static final List<Character> XLetterKey = Arrays.asList(new Character[] {'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M' });
	protected static final List<Character> symbolKey = Arrays.asList(new Character[] { '*','\\','>','-',']','{','/','!',',','<','?','~','}','&','@','#','[','.',':','+','|','`','%','\'' });

	public static boolean binarySearch(char[] a, char c) {
		for (char cc : a) {
			if (cc == c) {
				return true;
			}
		}
		return false;
	}

	public static int size(char c) {
		if (binarySearch("lijtf".toCharArray(), c)) {
			return 30;
		}
		if (binarySearch("wm".toCharArray(), c)) {
			return 20;
		}
		if (binarySearch("qeryuopasdghkzxcvbn".toCharArray(), c)) {
			return 25;
		}
		if (binarySearch("I".toCharArray(), c)) {
			return 33;
		}
		if (binarySearch("W".toCharArray(), c)) {
			return 18;
		}
		if (binarySearch("M".toCharArray(), c)) {
			return 20;
		}
		if (binarySearch("QERTYUOPASDFGHJKLZXCVBN".toCharArray(), c)) {
			return 23;
		}
		if (binarySearch("@%".toCharArray(), c)) {
			return 17;
		}
		if (binarySearch("|:'\\/`!*.]".toCharArray(), c)) {
			return 30;
		}
		if (binarySearch(">-{,<?~}&#[+".toCharArray(), c)) {
			return 25;
		}
		return 20;
	}

	public static String createLetterKeyboard(List<Character> letter, char c) throws IOException {
		BufferedImage image = ImageIO.read(new ByteArrayInputStream((byte[]) imageCache.get(c + "LetterKeyboard")));
//		Graphics g = image.getGraphics();
		Graphics2D g = image.createGraphics();  
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		
		g.setColor(Color.black);
		g.setFont(new Font("Arial", 1, Config.standardFontSize));
		int w = 73;
		int j = 4;
		int index = 0;
		for (int i = 0; i < 10; i++) {
			g.drawString(letter.get(index) + "", i * w + i * j + size(((Character) letter.get(index)).charValue()), 70);
			index++;
		}
		for (int i = 0; i < 9; i++) {
			g.drawString(letter.get(index) + "", 38 + i * w + i * j + size(((Character) letter.get(index)).charValue()),180);
			index++;
		}
		for (int i = 0; i < 7; i++) {
			g.drawString(letter.get(index) + "",115 + i * w + i * j + size(((Character) letter.get(index)).charValue()), 290);
			index++;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ImageIO.write(image, "png", out);
		byte[] b = out.toByteArray();
		return Base64.encode(b);
	}

	public static String createExtendNumberKeyboard(List<Integer> num, List<Character> extendnumberKey)
			throws IOException {
		BufferedImage image = ImageIO.read(new ByteArrayInputStream((byte[]) imageCache.get("numberKeyboard1")));
		Graphics g = image.getGraphics();
		g.setColor(Color.black);
		g.setFont(new Font("Arial", 1, Config.standardFontSize));
		int w = 144;
		int j = 4;
		int index = 0;
		for (int i = 0; i < 5; i++) {
			g.drawString(num.get(index) + "", 15 + i * w + i * j + w / 2 - 10, 70);
			index++;
		}
		for (int i = 0; i < 5; i++) {
			g.drawString(num.get(index) + "", 15 + i * w + i * j + w / 2 - 10, 180);
			index++;
		}
		index = 0;
		w = 73;
		j = 4;
		for (int i = 0; i < 7; i++) {
			g.drawString(extendnumberKey.get(index) + "",115 + i * w + i * j + size(((Character) extendnumberKey.get(index)).charValue()), 290);
			index++;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ImageIO.write(image, "png", out);
		byte[] b = out.toByteArray();
		return Base64.encode(b);
	}

	public static String createSymbolKeyboard(List<Character> symbolKey) throws IOException {
		BufferedImage image = ImageIO.read(new ByteArrayInputStream((byte[]) imageCache.get("symbolKeyboard")));
		Graphics g = image.getGraphics();
		g.setColor(Color.black);
		g.setFont(new Font("Arial", 1, Config.standardFontSize));
		int w = 73;
		int j = 4;
		int index = 0;
		for (int i = 0; i < 10; i++) {
			g.drawString(symbolKey.get(index) + "",i * w + i * j + size(((Character) symbolKey.get(index)).charValue()), 70);
			index++;
		}
		j = 5;
		for (int i = 0; i < 9; i++) {
			g.drawString(symbolKey.get(index) + "",38 + i * w + i * j - i * 1 + size(((Character) symbolKey.get(index)).charValue()), 180);

			index++;
		}
		j = 4;
		for (int i = 0; i < 5; i++) {
			g.drawString(symbolKey.get(index) + "",	188 + i * w + i * j + size(((Character) symbolKey.get(index)).charValue()), 290);
			index++;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ImageIO.write(image, "png", out);
		byte[] b = out.toByteArray();
		return Base64.encode(b);
	}
}
